//菜单新增/编辑
var id, pid, level, menuCode, assumeOffice;
$(function () {
	$("#assumeOffice").val("2")
	var html = "";
	html = "<select class='set' id='teamId' onchange='changeTeam()'></select>";
	// console.log($("#assumeOffice").val())
	// if ($("#assumeOffice").val() == '2') {
	// 	//员工
	// 	html = "<select class='set' id='teamId' onchange='changeTeam()'></select>";
	// } else {
	// 	//部门经理
	// 	$("#workBox").hide();
	// 	html = "<div id='teamId' name='teamId' ly-bind='teamId'></div>";
	// }
	$("#teamType").empty();
	$("#teamType").append(html);
	$("#team").removeClass("hide");
	console.log($("#team"))
	assumeOffice = $("#assumeOffice").val();
	console.log($("#assumeOffice").val())
	team("");

	pid = ly.getUrlParam("pid") || '0';
	id = ly.getUrlParam("id") || '';
	var t = 1;

	if(window.localStorage.getItem("backstage-access-teamId") == 'undefined'){
		teamId = '';
	}else{
		teamId = window.localStorage.getItem("backstage-access-teamId");
	}
	if (id) {
		console.log("进入编辑")
		$(".title span,title").html("编辑");
		$("#save-next-btn").remove();
		request({
			url: site.gateUrl + '/usersManagement/selectUsersById',
			data: { 'uid': id },
			ajaxform: true,
			type: 'get',
			callback: function (r) {
				if (r.code == "rest.success") {
					var d = r.result;
					gl(d.userType);
					chioceType(d.userType);
					$("#isUsing").getSelect({
						type: "radio",
						asName: "isUsing",
						list: [
							{ value: "1", display: "启用" },
							{ value: "0", display: "禁用" }
						],
						default: d.isUsing
					});
					$("#sex").getSelect({
						type: "radio",
						asName: "sex",
						list: [
							{ value: "1", display: "男" },
							{ value: "2", display: "女" }
						],
						default: d.sex
					});
					gl(d.userType, 2);
					//$("#userType").attr("disabled",false);
					$("#assumeOffice").attr("disabled", "disabled");
					var teamIdThis = "";
					if (d.userTeamList != null) {
						$.each(d.userTeamList, function (i, o) {
							teamIdThis += o.teamId + ",";
						});
						if (teamIdThis != null && teamIdThis != '') {
							teamIdThis = teamIdThis.substring(0, teamIdThis.lastIndexOf(","));
						}
					}
					assumeOffice = d.assumeOffice;
					var html = "";
					if (d.assumeOffice == 1) {
						html = "<div id='teamId' name='teamId' ly-bind='teamId'></div>";
						$("#teamType").empty();
						$("#teamType").append(html);
						team(teamIdThis);
					} else if(d.assumeOffice == 2){
						html = "<select class='set' id='teamId' onchange='changeTeam()'></select>";
						$("#teamType").empty();
						$("#teamType").append(html);
						team(teamIdThis);
						gz(teamIdThis, d.workId);

						$("#team").removeClass("hide");
					}
					$("#team").removeClass("hide");
					$("#nickName").val(d.nickName);
					$("#phone").val(d.phone);
					$("#identityCard").val(d.identityCard)
					$('#workerNum').val(d.workerNum)
					$("#emailAddress").val(d.emailAddress);
					$("#assumeOffice").val(d.assumeOffice);
				} else {
					ly.toast(r.msg);
				}
			}
		});
	} else {
		//team();
		console.log("进入添加")
		gl(1, 1);
		$("#ScenicArea").hide();
		chioceType(1);
		$("#isUsing").getSelect({
			type: "radio",
			asName: "isUsing",
			list: [
				{ value: "1", display: "启用" },
				{ value: "0", display: "禁用" }
			],
			default: 1
		});
		$("#sex").getSelect({
			type: "radio",
			asName: "sex",
			list: [
				{ value: "1", display: "男" },
				{ value: "2", display: "女" }
			],
			default: 1
		});
	}
	//提交回到列表页
	$("body").on("click", ".save-btn", function () {
		event.preventDefault();
		var is_id = $(this).attr("id"),
			add_url = "user.html";
		if (is_id == 'save-next-btn') {
			add_url = "add-user.html"
		}
		var success = $('#form').validate('submitValidate');
		/*if(personnelstyle=='2' || personnelstyle==2){
			
		}else{
			teamId=$("#teamId").val();
			//userType=$("#userType")
		}*/
		if (success) {
			var myreg=/^[1][0,1,2,3,4,5,6,7,8,9][0-9]{9}$/;
			if (!myreg.test($("#phone").val())) {
				ly.toast("请输入正确的手机号");
				return false;
			}
			if ($.trim($("#assumeOffice").val()) == '') {
				ly.toast("请选择职务");
				return false;
			}
			var userTeamList = new Array();
			if (assumeOffice == 1) {
				var obj = {};
				$("input[name=teamId]:checked").each(function () {
					obj = {
						teamId: $(this).val(),
						assumeOffice: 1
					}
					userTeamList.push(obj);
					obj = {};
				});
			} else {
				if ($.trim($("#teamId").val()) == "") {
					ly.toast("请选择部门");
					return false;
				}
				if ($.trim($("#workId").val()) == '') {
					ly.toast("请选择工种");
					return false;
				}
				var obj = {
					teamId: $("#teamId").val(),
					assumeOffice: 2
				}
				userTeamList.push(obj);
			}

			/*if (id) { // 编辑
				request({
					url: site.gateUrl + '/usersManagement/notarizeChangeDepartmentManager',
					data: {
						id: id,
						nickName: $("#nickName").val(),
						teamId: $("#teamId").find("input[name=teamId]:checked").val(),
					},
					type: 'post',
					callback: function (r) {
						if (r.code == "rest.success") {
							var result = r.result;
							if (result.popout) {
								ly.confirm(result.popoutHint, function() {
									$(".form").submitForm({
										id: id,
										originalDirectorId: result.originalDirectorId,
										userTeamList: userTeamList,
										userType: $("input[name=userType]:checked").val(),
										workId: $("#workId").val(),
										identityCard:$("#identityCard").val(),
										assumeOffice: $("#assumeOffice").val(),
										workerNum: $("#workerNum").val(),
										url: site.gateUrl + "/usersManagement/insertOrUpdateUser",
										ajaxform: false,
									}, add_url); //成功后跳转的页面,可为空
								});
							} else {
								$(".form").submitForm({
									id: id,
									userTeamList: userTeamList,
									userType: $("input[name=userType]:checked").val(),
									workId: $("#workId").val(),
									identityCard:$("#identityCard").val(),
									assumeOffice: $("#assumeOffice").val(),
									workerNum: $("#workerNum").val(),
									url: site.gateUrl + "/usersManagement/insertOrUpdateUser",
									ajaxform: false,
								}, add_url); //成功后跳转的页面,可为空
							}
						} else {
							ly.toast(r.msg || r.desc);
						}
					}
				});
			} else {
				request({
					url: site.gateUrl + '/usersManagement/notarizeChangeDepartmentManager',
					data: {
						nickName: $("#nickName").val(),
						teamId: $("#teamId").find("input[name=teamId]:checked").val(),
					},
					type: 'post',
					callback: function (r) {
						if (r.code == "rest.success") {
							var result = r.result;
							if (result.popout) {
								ly.confirm(result.popoutHint, function() {
									$(".form").submitForm({
										originalDirectorId: result.originalDirectorId,
										userTeamList: userTeamList,
										userType: $("input[name=userType]:checked").val(),
										workId: $("#workId").val(),
										identityCard:$("#identityCard").val(),
										workerNum: $("#workerNum").val(),
										assumeOffice: $("#assumeOffice").val(),
										url: site.gateUrl + "/usersManagement/insertOrUpdateUser",
										ajaxform: false,
									}, add_url); //成功后跳转的页面,可为空
								});
							} else {
								$(".form").submitForm({
									userTeamList: userTeamList,
									userType: $("input[name=userType]:checked").val(),
									workId: $("#workId").val(),
									identityCard:$("#identityCard").val(),
									workerNum: $("#workerNum").val(),
									assumeOffice: $("#assumeOffice").val(),
									url: site.gateUrl + "/usersManagement/insertOrUpdateUser",
									ajaxform: false,
								}, add_url); //成功后跳转的页面,可为空
							}
						} else {
							ly.toast(r.msg || r.desc);
						}
					}
				});
			}*/
			if (id) { // 编辑
				$(".form").submitForm({
					id: id,
					//teamId:teamId,
					userTeamList: userTeamList,
					userType: $("input[name=userType]:checked").val(),
					workId: $("#workId").val(),
					identityCard:$("#identityCard").val(),
					assumeOffice: $("#assumeOffice").val(),
					workerNum: $("#workerNum").val(),
					url: site.gateUrl + "/usersManagement/insertOrUpdateUser",
					ajaxform: false,
				}, add_url); //成功后跳转的页面,可为空

			} else {
				$(".form").submitForm({
					//teamId:teamId,
					userTeamList: userTeamList,
					userType: $("input[name=userType]:checked").val(),
					workId: $("#workId").val(),
					identityCard:$("#identityCard").val(),
					workerNum: $("#workerNum").val(),
					assumeOffice: $("#assumeOffice").val(),
					url: site.gateUrl + "/usersManagement/insertOrUpdateUser",
					ajaxform: false,
				}, add_url); //成功后跳转的页面,可为空
			}
		}
	})
	$("#teamId").change(function () {
		var id = $(this).val();
		gz(id);
	});

	// $("body").on("change", "#assumeOffice", function () {
	// 	var value = $(this).val();
	// 	var html = "";
	// 	if (value == '2') {
	// 		//员工
	// 		html = "<select class='set' id='teamId' onchange='changeTeam()'></select>";
	// 	} else {
	// 		//部门经理
	// 		$("#workBox").hide();
	// 		html = "<div id='teamId' name='teamId' ly-bind='teamId'></div>";
	// 	}
	// 	$("#teamType").empty();
	// 	$("#teamType").append(html);
	// 	$("#team").removeClass("hide");
	// 	assumeOffice = value;
	// 	team("");
	// });
	//通过用户类型切换职务选项
	$("body").on("click", "#userType input[name='userType']", function () {
		var value = $(this).val();
		var value1 = '';
		if(value == '1'){
			$("#assumeOffice").val('2');
			value1 = '2'
		}else{
			$("#assumeOffice").val('1');
			value1 = '1'
		}

		chioceType(value);
		var html = "";
		if (value1 == '2') {
			//员工
			html = "<select class='set' id='teamId' onchange='changeTeam()'></select>";
		} else {
			//部门经理
			$("#workBox").hide();
			html = "<div id='teamId' name='teamId' ly-bind='teamId'></div>";
		}
		$("#teamType").empty();
		$("#teamType").append(html);
		$("#team").removeClass("hide");
		assumeOffice = value1;
		team("");
	});
});
function chioceType(value) {
	if (value == 1) {
		$("#workBox").hide();
		$("#manager").hide();
		$("#staff").removeAttr('style');
	} else {
		$("#team").addClass("hide");
		$("#staff").hide();
		$("#manager").removeAttr('style');
	}
}

function gl(val, type) {
	if (type == '2' || type == 2) {
		$("#userType").getSelect({
			type: "radio",
			asName: "userType",
			list: [
				{ value: "1", display: "员工端" },
				{ value: "2", display: "管理端" }
			],
			default: val,
			disabled: true
		});
		//z(teamId);
	} else {
		$("#userType").getSelect({
			type: "radio",
			asName: "userType",
			list: [
				{ value: "1", display: "员工端" },
				{ value: "2", display: "管理端" }
			],
			default: val
		});
	}
}
function team(id) {
	request({
		url: site.gateUrl + '/back/team/selectTeamList',
		data: {
			"num": 1,
			"size": 100,
			"isUsing": '1'
		},
		ajaxform: true,
		type: 'get',
		callback: function (r) {
			if (r.code == "rest.success") {
				if (assumeOffice == 2) {
					var list = r.result.list, html = '<option value="">请选择</option>';
					for (var i = 0; i < list.length; i++) {
						if(id){
							html += '<option value="' + list[i].id + '">' + list[i].teamName + '</option>'
						}else{
							if(list[i].teamName!='文旅资源管理员'){
								html += '<option value="' + list[i].id + '">' + list[i].teamName + '</option>'
							}
							
						}
						
					}
					console.log(assumeOffice)
					$("#teamId").html(html);
					if (id != '' && id != null) {
						$("#teamId").val(id);
					}
				} else {
					console.log(assumeOffice)
					var list = new Array();
					var obj = {};
					$.each(r.result.list, function (i, o) {
						obj = {
							value: o.id,
							display: o.teamName
						};
						list.push(obj);
						obj = {};
					});
					$("#teamId").getSelect({
						// type: "checkbox",
						type: "radio",
						asName: "teamId",
						list: list,
						default: id
					});
				}

			} else {
				ly.toast(r.msg);
			}
		}
	});
}

function gz(id, workid) {//工种
	request({
		url: site.gateUrl + '/back/team/selectTeamWorkerListByTeamId',
		data: { 'id': id },
		ajaxform: true,
		type: 'get',
		callback: function (r) {
			if (r.code == "rest.success") {
				$("#workBox").show();
				var list = r.result,
					html = '<option value="">请选择</option>';

				for (var i = 0; i < list.length; i++) {
					if (list[i].workerId == workid) {
						html += '<option value="' + list[i].workerId + '" selected>' + list[i].workerName + '</option>'
					} else {
						html += '<option value="' + list[i].workerId + '">' + list[i].workerName + '</option>'
					}

				}
				$("#workId").html(html);
				//					if(workid!=null || workid!=''){
				//						
				//						$("#workId").val(workid);
				//					}
			} else {
				ly.toast(r.msg);
			}
		}
	});
}


function changeTeam() {
	gz($("#teamId").val());
}

// 确认是否换部门负责人
function confirmChangeDepartmentManager(data) {
	request({
		url: site.gateUrl + '/usersManagement/notarizeChangeDepartmentManager',
		data: data,
		type: 'post',
		callback: function (r) {
			if (r.code == "rest.success") {
				var result = r.result;
				console.log(result);
			} else {
				ly.toast(r.msg);
			}
		}
	});
}